package com.aipartner.controller;

import com.aipartner.common.PageResult;
import com.aipartner.common.Result;
import com.aipartner.dto.PaymentResponse;
import com.aipartner.dto.VipInfo;
import com.aipartner.dto.VipPlanResponse;
import com.aipartner.dto.VipPurchaseRequest;
import com.aipartner.entity.VipOrder;
import com.aipartner.service.VipService;
import com.aipartner.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * VIP Management Controller
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@RestController
@RequestMapping("/api/vip")
@RequiredArgsConstructor
@Validated
@Slf4j
@Api(tags = "VIP Management")
public class VipController {
    
    private final VipService vipService;
    
    /**
     * Get VIP plans
     */
    @GetMapping("/plans")
    @ApiOperation("Get all VIP plans")
    public Result<List<VipPlanResponse>> getVipPlans() {
        try {
            List<VipPlanResponse> plans = vipService.getVipPlans();
            return Result.success(plans);
        } catch (Exception e) {
            log.error("Error getting VIP plans", e);
            return Result.fail("Failed to get VIP plans");
        }
    }
    
    /**
     * Get user VIP info
     */
    @GetMapping("/info")
    @ApiOperation("Get current user VIP information")
    public Result<VipInfo> getUserVipInfo() {
        try {
            Long userId = UserContext.getCurrentUserId();
            if (userId == null) {
                return Result.fail("User not logged in");
            }
            
            VipInfo vipInfo = vipService.getUserVipInfo(userId);
            return Result.success(vipInfo);
        } catch (Exception e) {
            log.error("Error getting user VIP info", e);
            return Result.fail("Failed to get VIP information");
        }
    }
    
    /**
     * Purchase VIP
     */
    @PostMapping("/purchase")
    @ApiOperation("Purchase VIP subscription")
    public Result<PaymentResponse> purchaseVip(@Valid @RequestBody VipPurchaseRequest request) {
        try {
            Long userId = UserContext.getCurrentUserId();
            if (userId == null) {
                return Result.fail("User not logged in");
            }
            
            request.setUserId(userId);
            PaymentResponse response = vipService.createVipOrder(request);
            return Result.success(response);
        } catch (Exception e) {
            log.error("Error purchasing VIP", e);
            return Result.fail("VIP purchase failed");
        }
    }
    
    /**
     * Get user VIP orders
     */
    @GetMapping("/orders")
    @ApiOperation("Get user VIP orders")
    public Result<PageResult<VipOrder>> getUserOrders(
            @ApiParam("Current page") @RequestParam(defaultValue = "1") @Min(1) Long current,
            @ApiParam("Page size") @RequestParam(defaultValue = "10") @Min(1) Long size) {
        try {
            Long userId = UserContext.getCurrentUserId();
            if (userId == null) {
                return Result.fail("User not logged in");
            }
            
            PageResult<VipOrder> orders = vipService.getUserOrders(userId, current, size);
            return Result.success(orders);
        } catch (Exception e) {
            log.error("Error getting user orders", e);
            return Result.fail("Failed to get order list");
        }
    }
    
    /**
     * Payment callback
     */
    @PostMapping("/callback")
    @ApiOperation("Payment callback")
    public Result<String> paymentCallback(
            @ApiParam("Order number") @RequestParam String orderNo,
            @ApiParam("Transaction ID") @RequestParam String transactionId,
            @ApiParam("Payment status") @RequestParam String status) {
        try {
            boolean success = vipService.handlePaymentCallback(orderNo, transactionId, status);
            return success ? Result.success("Payment processed successfully") 
                          : Result.fail("Payment processing failed");
        } catch (Exception e) {
            log.error("Error processing payment callback", e);
            return Result.fail("Payment callback processing failed");
        }
    }
}